package com.hehe.boot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hehe.boot.pojo.Clinic;
import com.hehe.boot.pojo.ClinicHttp.ClinicRegisterRequest;
import com.hehe.boot.pojo.ClinicHttp.ClinicRegisterResponse;
import com.hehe.boot.pojo.ClinicHttp.ClinicShowDetailResponse;
import com.hehe.boot.pojo.Drug;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.service.ClinicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/clinic")
public class ClinicController {

    @Autowired
    private ClinicService clinicService;

    @PostMapping("/register")
    public Result<ClinicRegisterResponse> Register(@RequestBody ClinicRegisterRequest request){
        Long clinicId = clinicService.register(request);
        ClinicRegisterResponse response = new ClinicRegisterResponse();
        response.setClinicId(clinicId);
        return Result.success(response);
    }

    @GetMapping("/show")
    public Result<ClinicShowDetailResponse> ShowDetail(@RequestParam Integer clinicId){
        return Result.success(clinicService.showDetailByClinicId(clinicId));
    }

    @GetMapping("/list")
    public Result<List<Clinic>> List(){
        return Result.success(clinicService.list());
    }

    @GetMapping("like")
    public Result<List<Clinic>> Like (@RequestParam String name){
        return Result.success(clinicService.like(name));
    }

    @GetMapping("/find")
    public Result<IPage<Clinic>> findall(@RequestParam(required = false) String query,
                                       @RequestParam Integer page){
        if (query != null && !query.isEmpty()) {
            return Result.success(clinicService.getProPageLike(query,page));
        }
        return Result.success(clinicService.getProPage(page));
    }

    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Clinic drug){
        return Result.success(clinicService.insertDrug(drug));
    }

    @PostMapping("/update")
    public Result update(@RequestBody Clinic drug){
        return Result.success(clinicService.updateDrug(drug));
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Clinic drug){
        return Result.success(clinicService.deleteDrug(drug));
    }
}
